/**
 * Created by Churcy on 2017/1/9.
 */
/*
 * $(document).ready(function() { Equipment.searchEquipment() });
 */
window.onload = function () {
    Equipment.searchEquipment()
};
var Equipment = {

    searchEquipment: function () {
        $("#searchBTN").on('click', function () {
            var conditions = getCurrentConditions();
            conditions.pageNo = 1;
            ajaxSearchEquipmentList(conditions);
        });
        $("#resetBTN").on('click', function () {
            resetFn();
        });
        $("#addBTN").on('click', function () {
            var carryCode = $('#carryCode').val();
            var deviceId = $('#deviceId').val();
            var isChan = $('#isChan').val();
            var lineName = $('#lineName').val();
            var lineShort = $('#lineShort').val();
            var gprsCard = $('#gprsCard').val();
            var perAmount = $('#perAmount').val();
            var pageNo = $("#p").val();
            var APP_KEY = $('#APP_KEY').val();
            var conditions = {
                carryCode: encodeURI(carryCode),
                isChan: encodeURI(isChan),
                deviceId: encodeURI(deviceId),
                lineName: encodeURI(lineName),
                lineShort: encodeURI(lineShort),
                gprsCard: gprsCard,
                perAmount: perAmount,
                appKey: APP_KEY,
                pageIndex: pageNo
            };
            $.ajax({
                type: "GET",
                url: PROJECT_CTX + "/traffic/equipment/add",
                data: conditions,
                dataType: "json",
                success: function (data) {
                    window.location.href = PROJECT_CTX + "/traffic/equipment?appKey=" + APP_KEY;
                }
            })
        });
    }
}

function searchFn() {
    var conditions = getCurrentConditions();
    console.log("searchFN")
    var response = ajaxSearchEquipmentList(conditions);
}
// 重置查询条件
function resetFn() {
    $('#vehNo').val("");
    $('#deviceId').val("");
    $('#lineName').val("");
}

function getCurrentConditions() {
    console.log("getCurrentConditions");
    var vehNo = $('#vehNo').val();
    var deviceId = $('#deviceId').val();
    var lineName = $('#lineName').val();
    var pageNo = $("#p").val();
    var APP_KEY = $('#APP_KEY').val();
    var conditions = {

        vehNo: encodeURI(vehNo),
        deviceId: encodeURI(deviceId),
        lineName: encodeURI(lineName),
        appKey: APP_KEY,
        pageIndex: pageNo
    };
    return conditions;

}
function ajaxSearchEquipmentList(conditions) {
    console.log("ajaxSearchHospitalList");
    $.ajax({
        type: "GET",
        url: PROJECT_CTX + "/trafficback/deviceList",
        data: conditions,
        dataType: "json",
        success: function (data) {
            console.log(data)
            var result = eval(data);
            var resultCode = result.status.code;
            console.log(resultCode)
            console.log(result.line);
            if (1 == resultCode) {
                // 清空当前数据
                var equipmentList = $("#equipmentList").empty();
                // 填写查询出来的数据
                $.each(result.line, function (i, item) {
                    var tr = "<tr>";

                    tr += "<td >";
                    tr += item.carryCode;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.deviceId;
                    tr += "</td>"

                    /*tr += "<td >";
                     tr += item.isChan;
                     tr += "</td>"*/

                    tr += "<td >";
                    tr += item.lineName;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.lineShort;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.gprsCode;
                    tr += "</td>"

                    tr += "<td >";
                    tr += item.preMount;
                    tr += "</td>"

                    tr += "<td >";
                    tr += " <a title='修改' href='" + PROJECT_CTX + "/traffic/equipment/info?deviceId=" + item.deviceId + "&appKey=" + conditions.appKey + "'>修改</a>";
                    /*tr += " <a title='删除' href='#'>删除</a>";*/
                    tr += "</td >";

                    tr += "</tr>";
                    equipmentList.append(tr);
                    // 更新分页栏
                    reloadPage(result);
                });
            }

        }

        ,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(XMLHttpRequest.status);
            console.log(XMLHttpRequest.readyState);
            console.log(textStatus);
        }
    });
}

function ajaxCarryInfo() {

    var carryCode = $('#carryCode').val();
    var APP_KEY = $('#APP_KEY').val();
    var conditions = {
        carryCode: encodeURI(carryCode),
        appKey: APP_KEY
    }
    console.log(carryCode + "   " + APP_KEY);
    $.ajax({
        type: "GET",
        url: PROJECT_CTX + "/trafficback/getCarry",
        data: conditions,
        dataType: "json",
        success: function (data) {
            var result = eval(data);
            console.log(result);
            if (null != result.lineCode) {
                $("#lineShort").val(result.lineCode);
            }

            $("#perAmount").val(result.unitPrice / 100);
        }
    });
}
function deleteDevice(id, appKey,comCode) {
    swal({
            title: "确定删除么?",
            text: "删除后您将无法恢复,如需恢复请联系管理员!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定, 删除!",
            closeOnConfirm: false,
            showLoaderOnConfirm: true,
        },
        function () {
            setTimeout(function () {
                var conditions = {
                    id: id,
                    appKey: appKey,
                    comCode:comCode
                }
                $.ajax({
                    type: "GET",
                    data: conditions,
                    url: PROJECT_CTX + "/traffic/equipment/delete",
                    dataType: "json",
                    success: function (data) {
                        alert(1)
                    }
                })
                swal({
                        title: "已删除",
                        text: "未录入设备信息已删除",
                        type: "success"
                    },
                    function () {
                        window.location.href = PROJECT_CTX + "/traffic/equipment?appKey=" + appKey + "&comCode=" + comCode;
                    });

            }, 2000);
        });
}